package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.FixedDeteProMapper;
import com.indusfo.spc.pojo.FixedDetePro;
import com.indusfo.spc.service.FixedDeteProService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author louk
 * @date 2019/11/6 14:36
 */
@Service
public class FixedDeteProServiceImpl implements FixedDeteProService {

    private  static  final Logger logger = LoggerFactory.getLogger(FixedDeteProServiceImpl.class);


    @Resource
    private FixedDeteProMapper fixedDeteProMapper;

    @Override
    public JSONObject insertFixedDetePro(FixedDetePro fixedDetePro) {
        checkField(fixedDetePro);
        int insertFlag = fixedDeteProMapper.insertFixedDetePro(fixedDetePro);
        if(insertFlag>0){
            return JSONObject.oK("新增成功");
        }
        return JSONObject.oK("新增失败");
    }

    @Override
    public JSONObject updateFixedDetePro(FixedDetePro fixedDetePro) {
        checkField(fixedDetePro);
        int updateFlag = fixedDeteProMapper.updateFixedDetePro(fixedDetePro);
        if(updateFlag>0){
            return JSONObject.oK("编辑成功");
        }
        return JSONObject.oK("编辑失败");
    }

    @Override
    public JSONObject queryFixedDetePro(FixedDetePro fixedDetePro) {
        //声明listEquipment集合，用来装查询出来的参数
        List<FixedDetePro> listFixedDetePro = new ArrayList<>();
        try{
            Integer pagesize = fixedDetePro.getPagesize();
            Integer pageindex = fixedDetePro.getPageindex();
            if(pagesize != null && pageindex != null) {
                fixedDetePro.setIncept(pagesize*(pageindex - 1));
            }
            listFixedDetePro = fixedDeteProMapper.queryFixedDetePro(fixedDetePro);
            //判断查询出来的集合是否为空
            if(listFixedDetePro.isEmpty()) {
                return JSONObject.oK("没有设备资料相关数据", listFixedDetePro, 0);
            }
            //用来记录查询出来的条数
            int countNumber = fixedDeteProMapper.countByFixedDetePro(fixedDetePro);
            return JSONObject.oK("查询成功", listFixedDetePro, countNumber);
        }catch(GlobalException e){//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject defineFixedDetePro(Integer[] fixedDetePros, Integer dataState) {
        JSONObject jsonObject=new JSONObject();
        String resultStr="";
        if(fixedDetePros==null){
            resultStr="请选择固定检验项目";
        }
        if(dataState==null){
            resultStr="请选择状态";
        }
        int defineFlag = fixedDeteProMapper.defineFixedDetePro(fixedDetePros, dataState);
        if(defineFlag>0){
           switch (dataState){
               case 1:
                   resultStr="启用成功";
                   break;
               case 2:
                   resultStr="删除成功";
                   break;
               case 3:
                   resultStr="停用成功";
           }
        }else{
            switch (dataState){
                case 1:
                    resultStr="启用失败";
                    break;
                case 2:
                    resultStr="删除失败";
                    break;
                case 3:
                    resultStr="停用失败";
            }
        }
        return JSONObject.build(400,resultStr);
    }
    public void checkField(FixedDetePro fixedDetePro){
        //单据编号
        String receiptNumber = fixedDetePro.getReceiptNumber();
        //检验项目
        String detePro = fixedDetePro.getDetePro();
        String createTime = fixedDetePro.getCreateTime();
        Integer userId = fixedDetePro.getUserId();
        //下限
        Float lowerLimit = fixedDetePro.getLowerLimit();
        //上限
        Float upperLimit = fixedDetePro.getUpperLimit();
        //中心值
        Float middleLimit = fixedDetePro.getMiddleLimit();
        if(StringUtils.isBlank(receiptNumber)){
            //为null则抛出异常
            throw new ParamsErrorException("单据编号不能为空");
        }
        if(StringUtils.isBlank(detePro)){
            throw new ParamsErrorException("检验项目不能为空");
        }
        if(userId==null){
            throw new ParamsErrorException("制单人不能为空");
        }
        if(StringUtils.isBlank(createTime)){
            throw new ParamsErrorException("创建日期不能为空");
        }
        if(lowerLimit!=null && lowerLimit>10000){
            throw new ParamsErrorException("下限值过大");
        }
        if(upperLimit!=null && upperLimit>10000){
            throw new ParamsErrorException("上限值过大");
        }
        if(middleLimit!=null && middleLimit>10000){
            throw new ParamsErrorException("中间值过大");
        }
        int receiptNumberFlag = fixedDeteProMapper.checkReceiptNumberNoRepeat(fixedDetePro);
        if(receiptNumberFlag>0){
            throw new ParamsErrorException("单据编码已存在");
        }
    }
}
